package com.peach.trtc;

import android.content.Context;
import androidx.annotation.NonNull;
import io.flutter.embedding.engine.plugins.FlutterPlugin;
import io.flutter.embedding.engine.plugins.activity.ActivityAware;
import io.flutter.embedding.engine.plugins.activity.ActivityPluginBinding;
import io.flutter.plugin.common.BinaryMessenger;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugin.common.PluginRegistry.Registrar;
import io.flutter.plugin.platform.PlatformViewRegistry;

/**
 * TencentRtcPlugin
 */
public class TencentRtcPlugin implements FlutterPlugin, ActivityAware {

    public static final String CHANNEL = "plugins.peach.com/trtc";

    private MethodChannel methodChannel;

    private TrtcMethodCallHandler handler;


    public TencentRtcPlugin() {
    }


    // This static function is optional and equivalent to onAttachedToEngine. It supports the old
    // pre-Flutter-1.12 Android projects. You are encouraged to continue supporting
    // plugin registration via this function while apps migrate to use the new Android APIs
    // post-flutter-1.12 via https://flutter.dev/go/android-project-migration.
    //
    // It is encouraged to share logic between onAttachedToEngine and registerWith to keep
    // them functionally equivalent. Only one of onAttachedToEngine or registerWith will be called
    // depending on the user's project. onAttachedToEngine or registerWith must both be defined
    // in the same class.
    public static void registerWith(Registrar registrar) {
        if (registrar.activity() == null) { // from background service , like firebase
            return;
        }
        TencentRtcPlugin tencentRtcPlugin = new TencentRtcPlugin();
        tencentRtcPlugin.setUpChannel(registrar.context(), registrar.messenger(), registrar.platformViewRegistry());
    }

    @Override
    public void onAttachedToEngine(@NonNull FlutterPluginBinding flutterPluginBinding) {
        setUpChannel(flutterPluginBinding.getApplicationContext(), flutterPluginBinding.getBinaryMessenger(), flutterPluginBinding.getPlatformViewRegistry());
    }

    @Override
    public void onDetachedFromEngine(@NonNull FlutterPluginBinding binding) {
    }


    private void setUpChannel(Context context, BinaryMessenger messenger, PlatformViewRegistry platformViewRegistry) {
        methodChannel = new MethodChannel(messenger, CHANNEL);
        handler = new TrtcMethodCallHandler(messenger, context, methodChannel, platformViewRegistry);
        methodChannel.setMethodCallHandler(handler);
    }


    @Override
    public void onAttachedToActivity(@NonNull ActivityPluginBinding activityPluginBinding) {

    }

    @Override
    public void onDetachedFromActivityForConfigChanges() {

    }

    @Override
    public void onReattachedToActivityForConfigChanges(@NonNull ActivityPluginBinding activityPluginBinding) {

    }

    @Override
    public void onDetachedFromActivity() {

    }


}